﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LeadChina.AssemblyLine.Common.Computer
{
    public class MachineInfo
    {
        public static string ScanMachine(int cpuType = 1, int memoryType = 1, int diskType = 1)
        {
            string info = string.Empty;
            SystemInfo systemInfo = new SystemInfo();
            List<DiskInfo> ds = systemInfo.GetLogicalDrives();

            double[] vvs = systemInfo.GetCPUUsage();
            double cpuInfo;
            switch (cpuType)
            {
                //多核CPU中使用率最多的那个
                case 1:
                    cpuInfo = (from i in vvs orderby i descending select i).Max();
                    info += $"CPU使用率{cpuInfo.ToString("0.0")}%";
                    break;
                //多核CPU中使用率最少的那个
                case 2:
                    cpuInfo = (from i in vvs orderby i descending select i).Min();
                    info += $"CPU使用率{cpuInfo.ToString("0.0")}%";
                    break;
                //多核CPU中使用率平均值
                case 3:
                    cpuInfo = (from i in vvs orderby i descending select i).Average();
                    info += $"CPU使用率{cpuInfo.ToString("0.0")}%";
                    break;
            }
            switch (memoryType)
            {
                case 1:
                    info += $"  内存利用率:{(100 - systemInfo.MemoryAvailable * 100.0f / systemInfo.PhysicalMemory).ToString("0.0")}%";
                    break;
            }
            switch (diskType)
            {
                //显示所有磁盘可用空间
                case 1:
                    foreach (DiskInfo d in ds)
                    {
                        double f = d.FreeSpace / 1024.0f / 1024 / 1024;
                        info += $"  磁盘{d.DiskName}空闲空间为{f.ToString("0.0")}G";
                    }
                    break;
                //显示总磁盘可用空间
                case 2:
                    double fr = 0.0f;
                    foreach (DiskInfo d in ds)
                    {
                        double f = d.FreeSpace / 1024.0f / 1024 / 1024;
                        fr += f;
                    }
                    info += $"  磁盘合计空闲空间为{fr.ToString("0.0")}G";
                    break;
                //显示所有磁盘情况
                case 3:
                    foreach (DiskInfo d in ds)
                    {
                        double f1 = d.FreeSpace / 1024.0f / 1024 / 1024;
                        double f2 = d.Size / 1024.0f / 1024 / 1024;
                        info += $"  磁盘{d.DiskName}:{f1.ToString("0.0")}G/{f2.ToString("0.0")}G";
                    }
                    break;
                //显示总磁盘情况
                case 4:
                    double fr1 = 0.0f, fr2 = 0.0f;
                    foreach (DiskInfo d in ds)
                    {
                        double f1 = d.FreeSpace / 1024.0f / 1024 / 1024;
                        double f2 = d.Size / 1024.0f / 1024 / 1024;
                        fr1 += f1;
                        fr2 += f2;
                    }
                    info += $"  磁盘合计:{fr1.ToString("0.0")}G/{fr2.ToString("0.0")}G";
                    break;
            }
            return info;
        }

    }
}
